package com.allianz.webapps.controllers;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.allianz.frame.utils.PropertiesUtil;
import com.allianz.webapps.https.interfaces.util.HttpsUtil;

//http://localhost:8080/pad_marketing/main/pay.action
@Controller
@RequestMapping(value="main")
public class PayController {
	
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 跳转支付页面
	 */
	@RequestMapping(value="pay")
	public ModelAndView payment(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("views/page/pay");
		return mav;
	}
	
	@RequestMapping(value="ddapay")
	public ModelAndView paymentDda(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("views/page/ddapay");
		return mav;
	}
	
	@RequestMapping(value="payInit")
	@ResponseBody
	public Map<String, Object> payInit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		Map<String, Object> map = new HashMap<String,Object>();
		String jsonParam = request.getParameter("jsonParam");
		String resBody = HttpsUtil.httpPostByJsonParam(jsonParam, "app.getAppBase",false);
		JSONObject json = JSONObject.fromObject(resBody);
		if(!"S".equals(json.getString("Status")))
			throw new Exception(json.getString("ErrMsg"));
		map.put("data", json.get("Body"));
		
		String alipayUrl = PropertiesUtil.getProperty("alipayUrl");
		String wechatUrl = PropertiesUtil.getProperty("wechatUrl");
		map.put("alipayUrl", alipayUrl);
		map.put("wechatUrl", wechatUrl);
		return map;
	}
	
	@RequestMapping(value="initPayment")
	@ResponseBody
	public Map<String, Object> initPayment(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		Map<String, Object> map = new HashMap<String,Object>();
		String jsonParam = request.getParameter("jsonParam");
		String resBody = HttpsUtil.httpPostByJsonParam(jsonParam, "app.initPayment",false);
		JSONObject json = JSONObject.fromObject(resBody);
		if(!"S".equals(json.getString("Status")))
			throw new Exception(json.getString("ErrMsg"));
		map.put("data", json.get("Body"));
		
		String alipayUrl = PropertiesUtil.getProperty("alipayUrl");
		String wechatUrl = PropertiesUtil.getProperty("wechatUrl");
		map.put("alipayUrl", alipayUrl);
		map.put("wechatUrl", wechatUrl);
		return map;
	}

	@RequestMapping(value="payComplete")
	@ResponseBody
	public Map<String, Object> payComplete(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		Map<String, Object> map = new HashMap<String,Object>();
		String jsonParam = request.getParameter("jsonParam");
		String resBody = HttpsUtil.httpPostByJsonParam(jsonParam, "app.getAppBase",false);
		JSONObject json = JSONObject.fromObject(resBody);
		if(!"S".equals(json.getString("Status")))
			throw new Exception(json.getString("ErrMsg"));
		map.put("data", json.get("Body"));
		return map;
	}

	
}
